<?php
require_once 'auth.php';

if (isLoggedIn()) {
    header('Location: dashboard.php');
    exit;
}

$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (login($_POST['username'], $_POST['password'])) {
        header('Location: dashboard.php');
        exit;
    } else {
        $error = '用户名或密码错误';
    }
}
?>
<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>仓库管理系统 - 登录</title>
    <link rel="stylesheet" href="/dist/css/tabler.min.css">
</head>

<body class="d-flex flex-column">
    <div class="page page-center">
        <div class="container container-tight py-4">
            <div class="card card-md">
                <div class="card-body">
                    <h2 class="h2 text-center mb-4">仓库管理系统</h2>
                    <?php if ($error): ?>
                        <div class="alert alert-danger" role="alert">
                            <div class="alert-icon">
                                <!-- Download SVG icon from http://tabler.io/icons/icon/alert-circle -->
                                <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24"
                                    viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"
                                    stroke-linecap="round" stroke-linejoin="round"
                                    class="icon alert-icon icon-2">
                                    <path d="M3 12a9 9 0 1 0 18 0a9 9 0 0 0 -18 0" />
                                    <path d="M12 8v4" />
                                    <path d="M12 16h.01" />
                                </svg>
                            </div>
                            <div>
                                <h4 class="alert-heading">I'm so sorry&hellip;</h4>
                                <div class="alert-description">
                                <?= htmlspecialchars($error) ?>.
                                </div>
                            </div>
                        </div>                        
                    <?php endif; ?>
                    <form action="login.php" method="post" autocomplete="off">
                        <div class="mb-3">
                            <label class="form-label">用户名</label>
                            <input type="text" name="username" class="form-control" value="atacq" required autofocus>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">密码</label>
                            <input type="password" name="password" class="form-control" value="abc123456" required>
                        </div>
                        <div class="form-footer">
                            <button type="submit" class="btn btn-primary w-100">登录</button>
                        </div>
                    </form>
                    <p>admin : atacq</p>
                    <p>password : abc123</p>
                    <p>备注：以上账号密码仅为演示，实际使用时请修改密码。</p>
                    <p>如果忘记密码，请<a href="reset.php">重置</a></p>
                </div>
            </div>
        </div>
    </div>
    <script src="/dist/js/tabler.min.js"></script>
</body>

</html>